package com.ruoyi.controller.quality;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.quality.MesQualityStandard;
import com.ruoyi.service.quality.IMesQualityStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 质量检验标准Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/quality/standard")
public class MesQualityStandardController extends BaseController
{
    @Autowired
    private IMesQualityStandardService IMesQualityStandardService;

    /**
     * 查询质量检验标准列表
     */
    @PreAuthorize("@ss.hasPermi('quality:standard:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesQualityStandard mesQualityStandard)
    {
        startPage();
        List<MesQualityStandard> list = IMesQualityStandardService.selectMesQualityStandardList(mesQualityStandard);
        return getDataTable(list);
    }

    /**
     * 导出质量检验标准列表
     */
    @PreAuthorize("@ss.hasPermi('quality:standard:export')")
    @Log(title = "质量检验标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesQualityStandard mesQualityStandard)
    {
        List<MesQualityStandard> list = IMesQualityStandardService.selectMesQualityStandardList(mesQualityStandard);
        ExcelUtil<MesQualityStandard> util = new ExcelUtil<MesQualityStandard>(MesQualityStandard.class);
        util.exportExcel(response, list, "质量检验标准数据");
    }

    /**
     * 获取质量检验标准详细信息
     */
    @PreAuthorize("@ss.hasPermi('quality:standard:query')")
    @GetMapping(value = "/{standardId}")
    public AjaxResult getInfo(@PathVariable("standardId") Long standardId)
    {
        return success(IMesQualityStandardService.selectMesQualityStandardByStandardId(standardId));
    }

    /**
     * 新增质量检验标准
     */
    @PreAuthorize("@ss.hasPermi('quality:standard:add')")
    @Log(title = "质量检验标准", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesQualityStandard mesQualityStandard)
    {
        if ("1".equals(IMesQualityStandardService.checkStandardCodeUnique(mesQualityStandard.getStandardCode(),mesQualityStandard.getStandardId())))
        {
            return error("新增检验标准'" + mesQualityStandard.getStandardCode() + "'失败，检验标准编码已存在");
        }
        return toAjax(IMesQualityStandardService.insertMesQualityStandard(mesQualityStandard));
    }

    /**
     * 修改质量检验标准
     */
    /**
     * 修改质量检验标准
     */
    @PreAuthorize("@ss.hasPermi('quality:standard:edit')")
    @Log(title = "质量检验标准", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesQualityStandard mesQualityStandard)
    {
        if ("1".equals(IMesQualityStandardService.checkStandardCodeUnique(mesQualityStandard.getStandardCode(),mesQualityStandard.getStandardId())))
        {
            return error("修改检验标准'" + mesQualityStandard.getStandardCode() + "'失败，检验标准编码已存在");
        }
        return toAjax(IMesQualityStandardService.updateMesQualityStandard(mesQualityStandard));
    }

    /**
     * 删除质量检验标准
     */
    @PreAuthorize("@ss.hasPermi('quality:standard:remove')")
    @Log(title = "质量检验标准", businessType = BusinessType.DELETE)
    @DeleteMapping("/{standardIds}")
    public AjaxResult remove(@PathVariable Long[] standardIds)
    {
        return toAjax(IMesQualityStandardService.deleteMesQualityStandardByStandardIds(standardIds));
    }

    /**
     * 校验标准编码是否唯一
     */
    @GetMapping("/checkStandardCodeUnique")
    public AjaxResult checkStandardCodeUnique(String standardCode, Long standardId)
    {
        String result = IMesQualityStandardService.checkStandardCodeUnique(standardCode,standardId);
        return AjaxResult.success(result.equals("1")); // 返回true表示编码已存在，false表示编码可用
    }

    /**
     * 根据产品ID查询质量标准列表
     */
    @GetMapping("/product/{productId}")
    public AjaxResult getStandardsByProductId(@PathVariable Long productId)
    {
        List<MesQualityStandard> list = IMesQualityStandardService.selectStandardsByProductId(productId);
        return success(list);
    }

    /**
     * 根据工序ID查询质量标准列表
     */
    @GetMapping("/process/{processId}")
    public AjaxResult getStandardsByProcessId(@PathVariable Long processId)
    {
        List<MesQualityStandard> list = IMesQualityStandardService.selectStandardsByProcessId(processId);
        return success(list);
    }

    /**
     * 查询启用状态的质量标准列表
     */
    @GetMapping("/active")
    public AjaxResult getActiveStandards()
    {
        List<MesQualityStandard> list = IMesQualityStandardService.selectActiveStandards();
        return success(list);
    }
}